package com.cs.controller.article;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cs.controller.utils.R;
import com.cs.pojo.Article;
import com.cs.pojo.CarouselMap;
import com.cs.service.article.IArticleService;
import com.cs.service.carousel.ICarouselService;
import com.cs.service.image.ImageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private IArticleService iArticleService;
    @Autowired
    private ImageService imageService;

    /**
     * 添加文章推荐
     * @param files
     * @param article
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    @Transactional
    public R save(@RequestParam("file")MultipartFile[] files,@ModelAttribute Article article) throws Exception {
        ArrayList<String> list = imageService.uploadFileAvatar(files);
        article.setImages(list.toString());
        if(iArticleService.save(article)) {
            return new R("200", "上传成功");
        }else {
            return new R("400","上传失败");
        }
    }

    /**
     * 根据id修改文章推荐图
     * @param files
     * @param article
     * @return
     * @throws Exception
     */
    @PutMapping
    @Transactional
    public R update(@RequestParam(value = "file",required = false) MultipartFile[] files,@ModelAttribute Article article) throws Exception {
        ArrayList<String> resultImg=new ArrayList<>();
        String imgStr= article.getImages();
        System.out.println(imgStr+"-------------");
        String[] newImg;
        if(imgStr!=null&&!imgStr.equals("")){
            newImg=imgStr.substring(1, article.getImages().length()).split(",");
            for (String s : newImg) {
                if(s!=""){
                    int i = s.lastIndexOf("/");
                    String s1=s.substring(i+1);
                    resultImg.add(s1);
                }
            }
        }
        if(files!=null){
            ArrayList<String> list = imageService.uploadFileAvatar(files);
            for (String s : list) {
                resultImg.add(s);
            }
        }
        article.setImages(resultImg.toString());
        return new R("200",iArticleService.updateById(article),"修改成功");
    }

    /**
     * 根据id删除文章
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public R deleteArticle(@PathVariable Integer id){
        return new R("200",iArticleService.removeById(id));
    }

    /**
     * 分页获取文章推荐
     * @param currentPage
     * @param size
     * @return
     * @throws Exception
     */
    @GetMapping("/page/{currentPage}/{size}")
    public R getByPage(@PathVariable int currentPage,@PathVariable int size) throws Exception {
        IPage<Article> page = iArticleService.getPage(currentPage, size);
        if(currentPage>page.getPages()){
            page=iArticleService.getPage((int)page.getPages(),size);
        }
        return new R("200",page);
    }

    /**
     * 根据主题获取数据
     * @param title
     * @return
     */
    @GetMapping("/{title}")
    public R getByTopic(@PathVariable String title){
        List<Article> byTitle = iArticleService.getByTitle(title);
        return new R("200",byTitle);
    }

    /**
     * 根据主题获取分页数据
     * @param currentPage
     * @param size
     * @param title
     * @return
     * @throws Exception
     */
    @GetMapping("/page/{currentPage}/{size}/{title}")
    public R getByPageByTopic(@PathVariable int currentPage,@PathVariable int size,@PathVariable String title) throws Exception {
        IPage<Article> page = iArticleService.getPageByTitle(currentPage, size,title);
        if(currentPage>page.getPages()){
            page=iArticleService.getPage((int)page.getPages(),size);
        }
        return new R("200",page);
    }


    /**
     * 获取在用推荐文章（小程序使用）
     * @return
     */
    @GetMapping()
    public R getInUsing(){
        return new R("200",iArticleService.getInUsing());
    }

    /**
     * 设置文章使用状态
     * @param id
     * @return
     */
    @PutMapping("/update/{id}")
    public R setUsing(@PathVariable int id){
        return new R("200",iArticleService.setUsingArticle(id),"修改成功");
    }

}
